﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThuVienCT;
using System.Data.SqlClient;
using System.IO;


namespace QLCTNA
{
    public partial class Frm_hienthi : Form
    {
        private ConnectionData cnn = null;
        public Frm_hienthi()
        {
            InitializeComponent();
            FileText ft = new FileText("Config\\Config.ini");

            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            cnn = Connection.Connect();
        }

        private void Frm_hienthi_Load(object sender, EventArgs e)
        {
            string selection = null;

            if (cnn.Check())
            {
                selection = "select * from vw_congthuc";
                dgvhienthi.DataSource = cnn.SelectTable(selection);

            }
        }

        private void dgvhienthi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {


        }
          
        private void btnxemchitiet_Click(object sender, EventArgs e)
        {
          // lblma.Text = "";
           // lblten.Text = "";
           // lblphanloai.Text = "";


            if (dgvhienthi.Rows.Count > 0 && !string.IsNullOrEmpty(lblma.Text))
            {

                if (MessageBox.Show("ban co muon xem chi tiet khong?", "thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {


                    {
                        Frm_chitiet ct = new Frm_chitiet();
                        Crpchitiet rpt = new Crpchitiet();

                        // DataTable dt = cnn.SelectTable("select * from vw_congthucnauan where ma='" + lblma.Text + "'");

                        DataTable dt = cnn.SelectTable("select * from vw_CongThucNauAn where [Mã công thức]='" + lblma.Text + "'");
                        rpt.SetDataSource(dt);
                        ct.crtviewer.ReportSource = rpt;
                        ct.ShowDialog();
                      
                    }
                }

                else
                {
                    MessageBox.Show("ban chua chon cong thuc de xem");
                }
            }
            else
            {
                MessageBox.Show("ban chua chon cong thuc cu the");
            }
            
       
                }

        private void dgvhienthi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int dong;
            dong = e.RowIndex;
            txtma.Text = dgvhienthi.Rows[dong].Cells[0].Value.ToString();
            txtten.Text = dgvhienthi.Rows[dong].Cells[1].Value.ToString();
            txtpl.Text = dgvhienthi.Rows[dong].Cells[2].Value.ToString();
            //lblNguyenlieu.Text = dgvhienthi.Rows[dong].Cells[3].Value.ToString();
            //lblQuyTrinh.Text = dgvhienthi.Rows[dong].Cells[4].Value.ToString();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void loaddata()
        {
            string load = "select * from vw_congthuc";
            dgvhienthi.DataSource = cnn.SelectTable(load);
        }
        private void btnluu_Click(object sender, EventArgs e)
        {
            int kt = 0;
            {
                String update = "update vw_CongThuc set [Tên công thức]={0}, [Phân Loại] = {1} "
                    + "where [Mã Công Thức] like +'" + txtma.Text + "'";
                kt = cnn.Execute(update, txtten.Text, txtpl.Text);
                dgvhienthi.DataSource = cnn.SelectTable("select * from vw_CongThuc");

                if (kt > 0)
                {
                    MessageBox.Show("Đã lưu thành công!");
                    btnsua.Enabled = true;
                    btnluu.Enabled = false;
                    btnxemchitiet.Enabled = true;

                    dgvhienthi.DataSource = cnn.SelectTable("select * from vw_CongThuc");
                    
                }
                
         

            }
         
        }
        private void locktext()
        {
          //  txtma.Enabled = false;
            //txtten.Enabled = false;
          //  txtpl.Enabled = false;
            btnluu.Enabled = true;
            btnsua.Enabled = false;
        }

        private void un_locktext()
        {
            txtma.Enabled = true;
            txtten.Enabled = true;
            txtpl.Enabled = true;
            btnsua.Enabled = false;
            btnluu.Enabled = true;
        }
        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txtten.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn thông tin để sửa");

            }
            else
            {
            btnxemchitiet.Enabled = false;
            btnsua.Enabled = false;
            btnluu.Enabled = true;
            txtma.Enabled = false;
            txtten.Focus();
                }
        }
            }
        }
    

